package expriment3_bag;
import java.util.Scanner;

public class Nineteen {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int[] arr = {11, 34, 47, 19, 5, 87, 63, 88};
        System.out.println("原数组如下：");
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + "\t");
        }
        System.out.println();
        for (int i = 0; i < arr.length - 1; i++) {
            int min = i;
            for (int j = i; j < arr.length; j++) {
                if (arr[j] < arr[min]) {
                    min = j;
                }
            }
            int temp = arr[i];
            arr[i] = arr[min];
            arr[min] = temp;
        }
        System.out.println("请输入要查找的数：");
        int a = scanner.nextInt();
        int left = 0;
        int right = arr.length - 1;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (arr[mid] == a) {
                System.out.println(a + "在数组中的索引为：" + (mid+1));
                return;
            } else if (arr[mid] < a) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
        System.out.println("该数组中没有该元素：" + a);
    }
}